package com.allm.ai.common.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.allm.ai.common.entity.ConversationMessage;
import com.allm.ai.common.repository.ConversationMessageRepository;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@RequiredArgsConstructor
@Slf4j
public class ConversationMessageService {
    
    private final ConversationMessageRepository messageRepository;
    
    /**
     * 保存用户消息
     */
    @Transactional
    public ConversationMessage saveUserMessage(String conversationId, String modelConversationId, 
                                             Long modelId, String content, Integer messageOrder, Integer roundNumber) {
        ConversationMessage message = ConversationMessage.builder()
            .conversationId(conversationId)
            .modelConversationId(modelConversationId)
            .modelId(modelId)
            .role(ConversationMessage.MessageRole.USER)
            .content(content)
            .messageOrder(messageOrder)
            .roundNumber(roundNumber)
            .build();
        
        ConversationMessage saved = messageRepository.save(message);
        log.info("保存用户消息: conversationId={}, modelId={}, messageOrder={}, roundNumber={}", 
                conversationId, modelId, messageOrder, roundNumber);
        
        return saved;
    }
    
    /**
     * 保存AI回复消息
     */
    @Transactional
    public ConversationMessage saveAssistantMessage(String conversationId, String modelConversationId, 
                                                  Long modelId, String content, String thinkingContent, 
                                                  Integer messageOrder, Integer roundNumber) {
        ConversationMessage message = ConversationMessage.builder()
            .conversationId(conversationId)
            .modelConversationId(modelConversationId)
            .modelId(modelId)
            .role(ConversationMessage.MessageRole.ASSISTANT)
            .content(content)
            .thinkingContent(thinkingContent)
            .messageOrder(messageOrder)
            .roundNumber(roundNumber)
            .build();
        
        ConversationMessage saved = messageRepository.save(message);
        log.info("保存AI回复: conversationId={}, modelId={}, messageOrder={}, roundNumber={}", 
                conversationId, modelId, messageOrder, roundNumber);
        
        return saved;
    }
    
    /**
     * 获取对话的所有消息
     */
    public List<ConversationMessage> getConversationMessages(String conversationId) {
        return messageRepository.findByConversationIdOrderByRoundNumberAscMessageOrderAsc(conversationId);
    }
    
    /**
     * 根据模型对话ID获取消息
     */
    public List<ConversationMessage> getConversationMessagesByModelConversationId(String modelConversationId) {
        return messageRepository.findByModelConversationIdOrderByMessageOrderAsc(modelConversationId);
    }
    
    /**
     * 删除对话的所有消息
     */
    @Transactional
    public void deleteConversationMessages(String conversationId) {
        messageRepository.deleteByConversationId(conversationId);
        log.info("删除对话消息: conversationId={}", conversationId);
    }

    /**
     * 获取对话的下一个消息顺序
     */
    public Integer getNextMessageOrder(String conversationId) {
        return messageRepository.getNextMessageOrder(conversationId);
    }

    /**
     * 获取指定轮次的所有消息
     */
    public List<ConversationMessage> getRoundMessages(String conversationId, Integer roundNumber) {
        return messageRepository.findByConversationIdAndRoundNumberOrderByMessageOrderAsc(conversationId, roundNumber);
    }
}
